package com.example.administrator.szb.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseFragmentActivity;
import com.example.administrator.szb.fragments.shoucang.AnliShouCang;
import com.example.administrator.szb.fragments.shoucang.JGShouCang;
import com.example.administrator.szb.fragments.shoucang.ServiceShouCang;
import com.example.administrator.szb.fragments.shoucang.WDShouCang;
import com.example.administrator.szb.fragments.shoucang.XMShouCang;
import com.example.administrator.szb.fragments.shoucang.ZXShouCang;


public class ShouCangActivity extends MVPBaseFragmentActivity implements View.OnClickListener{
    private LinearLayout activity_guan_zhu,ll_container;
    private TextView bar_right,bar_title;
    public static boolean canShowCheckBox = false;
    private TextView tv_1;
    private TextView tv_2;
    private TextView tv_3;
    private TextView tv_4;
    private TextView tv_5;
    private TextView tv_6;
    private JGShouCang jgShouCang;
    private WDShouCang wdShouCang;
    private ZXShouCang zxShouCang;
    private XMShouCang xmShouCang;
    private ServiceShouCang serviceShouCang;
    private AnliShouCang anliShouCang;
    private int types = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shou_cang);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView() {
        types = getIntent().getIntExtra("type",0);
        ll_container = findViewById(R.id.ll_container);
        ll_container.setVisibility(View.GONE);
        activity_guan_zhu = (LinearLayout) findViewById(R.id.activity_guan_zhu);
        tv_1 = (TextView) findViewById(R.id.tv_1);
        tv_2 = (TextView) findViewById(R.id.tv_2);
        tv_3 = (TextView) findViewById(R.id.tv_3);
        tv_4 = (TextView) findViewById(R.id.tv_4);
        tv_5 = (TextView) findViewById(R.id.tv_5);
        tv_6 = (TextView) findViewById(R.id.tv_6);
        bar_title = findViewById(R.id.bar_title);
        bar_right = findViewById(R.id.bar_right);
        bar_title.setText("收藏管理");
        initTitle();
        bar_right.setText("编辑");
        bar_right.setVisibility(View.VISIBLE);
        bar_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (canShowCheckBox){
                    canShowCheckBox = false;
                    bar_right.setText("编辑");
                    //通知Fragment隐藏CheckBox
                }else {
                    canShowCheckBox = true;
                    bar_right.setText("取消");
                    //通知Fragment显示CheckBox
                }
                notifyFrgament();
            }
        });
    }
    private void initTitle(){
        switch (types){
            case 0:
                onClick(tv_4);
                bar_title.setText("收藏的项目");
                break;
            case 1:
                onClick(tv_2);
                bar_title.setText("收藏的问答");
                break;
            case 2:
                onClick(tv_1);
                bar_title.setText("收藏的机构");
                break;
            case 3:
                onClick(tv_3);
                bar_title.setText("收藏的资讯");
                break;
            case 4:
                onClick(tv_5);
                bar_title.setText("收藏的服务");
                break;
            case 5:
                onClick(tv_6);
                bar_title.setText("收藏的案例");
                break;
        }
    }
    public void notifyFrgament(){
        if (!canShowCheckBox){
            bar_right.setText("编辑");
        }
        if (jgShouCang!=null){
            jgShouCang.notifys();
        }
        if (wdShouCang!=null){
            wdShouCang.notifys();
        }
        if (zxShouCang!=null){
            zxShouCang.notifys();
        }
        if (xmShouCang!=null){
            xmShouCang.notifys();
        }
        if (serviceShouCang!=null){
            serviceShouCang.notifys();
        }
        if (anliShouCang!=null){
            anliShouCang.notifys();
        }
    }

    @Override
    protected void initEvent() {
        tv_1.setOnClickListener(this);
        tv_2.setOnClickListener(this);
        tv_3.setOnClickListener(this);
        tv_4.setOnClickListener(this);
        tv_5.setOnClickListener(this);
        tv_6.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        canShowCheckBox = false;
        initTitle();

    }

    @Override
    public void onClick(View v) {
        if (bar_right.getText().toString().equals("取消")){
            return;
        }
        FragmentTransaction transaction = getFragmentTransaction();
        reset();
        hideFragment(transaction);
        switch (v.getId()){
            case R.id.tv_1:
                if (jgShouCang==null){
                    jgShouCang = new JGShouCang();
                    transaction.add(R.id.frameLayout,jgShouCang);
                }else {
                    transaction.show(jgShouCang);
                }
                tv_1.setTextColor(getResources().getColor(R.color.basecolor));
                break;
            case R.id.tv_2:
                if (wdShouCang==null){
                    wdShouCang = new WDShouCang();
                    transaction.add(R.id.frameLayout,wdShouCang);
                }else {
                    transaction.show(wdShouCang);
                }
                tv_2.setTextColor(getResources().getColor(R.color.basecolor));
                break;
            case R.id.tv_3:
                if (zxShouCang==null){
                    zxShouCang = new ZXShouCang();
                transaction.add(R.id.frameLayout,zxShouCang);
            }else {
                transaction.show(zxShouCang);
            }
                tv_3.setTextColor(getResources().getColor(R.color.basecolor));

                break;
            case R.id.tv_4:
                if (xmShouCang==null){
                    xmShouCang = new XMShouCang();
                    transaction.add(R.id.frameLayout,xmShouCang);
                }else {
                    transaction.show(xmShouCang);
                }
                tv_1.setTextColor(getResources().getColor(R.color.basecolor));
                break;
            case R.id.tv_5:
                if (serviceShouCang==null){
                    serviceShouCang = new ServiceShouCang();
                    transaction.add(R.id.frameLayout,serviceShouCang);
                }else {
                    transaction.show(serviceShouCang);
                }
                tv_2.setTextColor(getResources().getColor(R.color.basecolor));
                break;
            case R.id.tv_6:
                if (anliShouCang==null){
                    anliShouCang = new AnliShouCang();
                    transaction.add(R.id.frameLayout,anliShouCang);
                }else {
                    transaction.show(anliShouCang);
                }
                tv_3.setTextColor(getResources().getColor(R.color.basecolor));

                break;
        }
        transaction.commitAllowingStateLoss();
    }


    private void hideFragment(FragmentTransaction transaction){
        if (jgShouCang!=null){
            transaction.hide(jgShouCang);
        }
        if (wdShouCang!=null){
            transaction.hide(wdShouCang);
        }
        if (zxShouCang!=null){
            transaction.hide(zxShouCang);
        }
        if (xmShouCang!=null){
            transaction.hide(xmShouCang);
        }
        if (serviceShouCang!=null){
            transaction.hide(serviceShouCang);
        }
        if (anliShouCang!=null){
            transaction.hide(anliShouCang);
        }
    }



    public void reset(){
        tv_1.setTextColor(getResources().getColor(R.color.gray1));
        tv_2.setTextColor(getResources().getColor(R.color.gray1));
        tv_3.setTextColor(getResources().getColor(R.color.gray1));
    }
}
